﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Infrastructure;
using System.Web.Mvc;
using Kaiser.Infrastructure.Modules;
using Kaiser.Infrastructure.User;
using Kaiser.Models;
using Kaiser.Modules.Admin.Repositories;
using Kaiser.Services;
using Kaiser.Services.Impl;

namespace Kaiser.Modules.Admin
{
    public class AdminModule : Module, IModuleMetaData, IAuthenticationModule
    {
        public override string ModuleName
        {
            get { return "Admin"; }
        }
        public override string AuthenticationModuleName
        {
            get
            {
                return "Admin";
            }
        }

        public override void Initialize()
        {
            SetupContainer();
        }

        /// <summary>
        /// 设置DI映射
        /// </summary>
        private void SetupContainer()
        {
            DependencyInjector.RegisterType<ISystemManageService, SystemManageService>();
            DependencyInjector.RegisterType<IAttachService, AttachService>();

            DependencyInjector
                // 系统(站点)配置信息处理逻辑
                //.RegisterType<ISiteService, SiteService>()
                //// 系统公告，用于系统后台的通知公告
                //.RegisterType<IBulletinService, BulletinService>()
                // 用户信息、用户组、用户角色、权限信息的处理逻辑
                .RegisterType<IUserService, UserService>()
                .RegisterType<IUserGroupService, UserGroupService>()
                .RegisterType<IRoleService, RoleService>()
                .RegisterType<IPermissionService, PermissionService>();

        }

        public override void RegisterRoutes()
        {

            MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}.aspx/{id}",
                new { action = "Index", id = UrlParameter.Optional }, new string[] { "Kaiser.Modules.Admin.Controllers" }
            );

        }

        public List<Kaiser.Models.ModuleMenuItem> GetModuleMenus()
        {
            return null;
        }

        public Infrastructure.User.IUser GetUser(System.Web.Routing.RequestContext context)
        {
            if (context.HttpContext.User.Identity.IsAuthenticated)
            {
                var userService = DependencyInjector.GetService<IUserService>();
                if (userService != null)
                {
                    var user = userService.GetItem(context.HttpContext.User.Identity.Name);
                    user.Identity = context.HttpContext.User.Identity;
                    return user;
                }
            }
          return new UserAnonymous();
        }

        public string GetRegistrationUrl(System.Web.Routing.RequestContext context)
        {
            throw new NotImplementedException();
        }

        public string GetSignInUrl(System.Web.Routing.RequestContext context)
        {
            throw new NotImplementedException();
        }

        public string GetSignOutUrl(System.Web.Routing.RequestContext context)
        {
            throw new NotImplementedException();
        }
    }

}
